/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.model.Ambulance;
import cc.altius.service.AmbulanceService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author sagar
 */
@Controller
//@SessionAttributes("ambulance")
public class AmbulanceController {

    @Autowired
    private AmbulanceService ambulanceService;

    @ModelAttribute("ambulanceList")
    List<Ambulance> getAmbulanceList() {
        return this.ambulanceService.getAmbulanceList();
    }

    @RequestMapping(value = "/ambulance/addAmbulance.htm", method = RequestMethod.GET)
    public String showAmbulancePage(ModelMap modelMap) {
        Ambulance ambulance = new Ambulance();
        modelMap.addAttribute("ambulance", ambulance);
        return "ambulance/addAmbulance";
    }

    @RequestMapping(value = "/ambulance/addAmbulance.htm", method = RequestMethod.POST)
    public String addAmbulancePage(@ModelAttribute("ambulance") Ambulance ambulance, Errors errors, SessionStatus sessionStatus) {
        if (this.ambulanceService.existAmbulanceByCompanyName(ambulance.getCompanyName())) {
            errors.rejectValue("companyName", "msg.duplicateCompany");
            return "ambulance/addAmbulance";
        }
        int ambulanceId = this.ambulanceService.addAmbulance(ambulance);
        System.out.println("ambulanceId:"+ambulanceId);
        if (ambulanceId == 0) {
            errors.rejectValue("companyName", "msg.ambulanceError");
            return "ambulance/addAmbulance";
        } else {
            sessionStatus.setComplete();
            return "redirect:addAmbulance.htm?msg=Succesfully Added Ambulance";
        }
    }

    @RequestMapping(value = "/ambulance/listAmbulance.htm", method = RequestMethod.GET)
    public String showAmbulanceListPage(ModelMap modelMap) {
        return "ambulance/listAmbulance";
    }

    @RequestMapping(value = "/ambulance/updateAmbulance.htm", method = RequestMethod.GET)
    public String showUdateAmbulancePage(@RequestParam(value = "ambulanceId") int ambulanceId, ModelMap modelMap) {
        Ambulance ambulance = this.ambulanceService.getAmbulanceById(ambulanceId);
        modelMap.addAttribute("ambulance", ambulance);
        return "ambulance/updateAmbulance";
    }

    @RequestMapping(value = "/ambulance/updateAmbulance.htm", method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute("ambulance") Ambulance ambulance, @RequestParam(value = "ambulanceId", required = true) int ambulanceId,
            SessionStatus sessionStatus, HttpServletRequest request) {
        //int ambulanceId1=ServletRequestUtils.getIntParameter(request, "ambulanceId",0);
        this.ambulanceService.updateAmbulance(ambulance, ambulanceId);
        sessionStatus.setComplete();
        return "redirect:listAmbulance.htm?msg=Succesfully Updated Ambulance";

    }
}
